package com.pangolin.pangolinim.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.ui.activity.SessionActivity;
import com.pangolin.pangolinim.ui.activity.UserInfoActivity;
import com.pangolin.pangolinim.ui.fragment.RecentMessageFragment;
import com.pangolin.pangolinim.utils.GsonUtil;

import java.util.List;

public class RecentMessageAdapter extends RecyclerView.Adapter<RecentMessageAdapter.ViewHolder>{
    private List<UISession> mRecentMsgList;
    private UIContact me;
    private RecentMessageFragment context;

    public RecentMessageAdapter(RecentMessageFragment context , List<UISession> recentMsgList , UIContact me){
        this.context = context;
        mRecentMsgList = recentMsgList;
        this.me = me;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView ivHeader;
        TextView tvName;
        TextView tvTime;
        TextView tvContent;
        LinearLayout llSession;
        TextView tvCount;
        public ViewHolder(View view){
            super(view);
            ivHeader = view.findViewById(R.id.ivHeader);
            tvName = view.findViewById(R.id.tvDisplayName);
            tvContent = view.findViewById(R.id.tvContent);
            tvTime = view.findViewById(R.id.tvTime);
            llSession = view.findViewById(R.id.llSession);
            tvCount = view.findViewById(R.id.tvCount);
        }
    }

    @Override
    public RecentMessageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recent_message , parent , false);
        final RecentMessageAdapter.ViewHolder holder = new RecentMessageAdapter.ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        UISession session = mRecentMsgList.get(holder.getAbsoluteAdapterPosition());
        if(session.getName() != null){
            holder.tvName.setText(session.getName());
        }
        if(session.getLastmsg() != null){
            if(session.getMsgtype() == UIMessage.MSG_TEXT){
                holder.tvContent.setText(session.getLastmsg());
            }else{
                holder.tvContent.setText("[图片]");
            }
        }
        if(session.getLasttime() != null){
            holder.tvTime.setText(session.getLasttime());
        }
        if(session.getHeadimg() != null){
            holder.ivHeader.setImageBitmap(session.getHeadimg());
        }
        if(session.getUnread() > 0){
            holder.tvCount.setVisibility(View.VISIBLE);
            holder.tvCount.setText(String.valueOf(session.getUnread()));
        }else{
            holder.tvCount.setVisibility(View.GONE);
            holder.tvCount.setText(String.valueOf(session.getUnread()));
        }
        //点击响应
        holder.llSession.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                //移除未读消息
                context.removeUnread(session.getUid());

                Intent intent = new Intent(view.getContext(), SessionActivity.class);
                UIContact contact = new UIContact();
                contact.setUid(session.getUid());
                contact.setName(session.getName());
                contact.setHeader(session.getHeader());
                contact.setBitHeader(session.getHeadimg());
                intent.putExtra("param1", GsonUtil.toJson(me));
                intent.putExtra("param2", GsonUtil.toJson(contact));
                view.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount(){
        return mRecentMsgList.size();
    }
}
